const path = require("path");
const ESLintPlugin = require("eslint-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');

const os = require("os");
// cpu核数
const threads = os.cpus().length;
module.exports = {
  entry: "./src/main.js",
  output: {
    path: undefined,
    filename: "static/js/main.js",
    clean: true
  },
  module: {
    rules: [
      {
        oneOf: [
          {
            test: /\.css$/,
            use: [
              "style-loader",
              "css-loader"
            ]
          },
          {
            test: /\.less$/,
            use: [
              "style-loader",
              "css-loader",
              "less-loader"
            ]
          },
          {
            test: /\.s[ac]ss$/,
            use: [
              "style-loader",
              "css-loader",
              "sass-loader"
            ]
          },
          {
            test: /\.styl$/,
            use: [
              "style-loader",
              "css-loader",
              "stylus-loader"
            ]
          },
          {
            test: /\.(png|jpe?g|gif|webp|svg)$/,
            type: "asset",
            parser: {
              dataUrlCondition: {
                maxSize: 10 * 1024
              }
            },
            generator: {
              filename: "static/images/[hash:10][ext][query]"
            }
          },
          {
            test: /\.(ttf|woff2?|mp3|mp4|avi)$/,
            type: "asset/resource",
            generator: {
              filename: "static/media/[hash:10][ext][query]"
            }
          },
          {
            test: /\.js$/,
            exclude: /node_modules/,
            // include:path.resolve(__dirname,"../src"),
            use: [
              {
                loader: "thread-loader",
                options: {
                  works: threads
                }
              },
              {
                loader: "babel-loader",
                options: {
                  //   presets: ['@babel/preset-env']
                  cacheDirectory: true,//开启cache缓存
                  cacheCompression: false//关闭缓存文件压缩
                }
              }
            ]
          }
        ]
      }

    ]
  },
  plugins: [
    new ESLintPlugin({
      context: path.resolve(__dirname, "../src"),
      exclude: "node_modules",
      cache: true,
      cacheLocation: path.resolve(__dirname, "../node_modules/.cache/eslint"),
      threads
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "../public/index.html")
    })
  ],
  // 开发服务器
  devServer: {
    host: "localhost", // 启动服务器域名
    port: "3000", // 启动服务器端口号
    open: true, // 是否自动打开浏览器
    hot: true,
  },
  devtool: "cheap-module-source-map",
  mode: "development"
}